package com.wt;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract.QuickContact;
import android.graphics.Rect;
import android.view.View;
import android.provider.ContactsContract.Contacts;

import com.android.contacts.list.DefaultContactBrowseListFragment;
import com.android.contacts.quickcontact.QuickContactActivity;
import com.android.contacts.interactions.ContactDeletionInteraction;
import com.android.contacts.list.OnContactBrowserActionListener;
import com.android.contacts.common.list.ContactListFilter;
import com.android.contacts.R;

import android.app.AlertDialog;
import android.content.DialogInterface;

import android.provider.ContactsContract;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

public class WtPeopleActivity extends Activity{
	private DefaultContactBrowseListFragment mAllFragment;
	private View mOptionMenu;
	
	@Override
	protected void onCreate(Bundle savedState) {
		super.onCreate(savedState);
		setContentView(R.layout.wt_people_activity);

		initOptionMenu();

		final FragmentManager fragmentManager = getFragmentManager();
		mAllFragment = (DefaultContactBrowseListFragment)fragmentManager.findFragmentById(R.id.wt_contacts_list);
		mAllFragment.setOnContactListActionListener(new ContactBrowserActionListener());
	}

	private void initOptionMenu(){
		View optButton = findViewById(R.id.wt_option_button);
		optButton.setOnClickListener(mOnClickListener);
		
		mOptionMenu = findViewById(R.id.wt_option_menu);
		View menuAdd = findViewById(R.id.wt_menu_add);
		menuAdd.setOnClickListener(mOnClickListener);
		View menuBack = findViewById(R.id.wt_menu_back);
		menuBack.setOnClickListener(mOnClickListener);
		View menuDelAll = findViewById(R.id.wt_menu_delall);
		menuDelAll.setOnClickListener(mOnClickListener);
	}

	private void showOptionMenu(){
		mOptionMenu.setVisibility(View.VISIBLE);
	}

	private void hideOptionMenu(){
		mOptionMenu.setVisibility(View.GONE);
	}

	private View.OnClickListener mOnClickListener = new View.OnClickListener(){
		@Override
		public void onClick(View v){
			final int id = v.getId();
			if(id == R.id.wt_option_button){
				showOptionMenu();
				return;
			}
			hideOptionMenu();
			switch(id){
				case R.id.wt_menu_back:
					break;
				case R.id.wt_menu_add:
					Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
					startActivity(intent);
					break;
				case R.id.wt_menu_delall:
					showDelAllDialog();
					break;
				default:
					break;
			}
		}
	};

	private AlertDialog mDelAllDialg;
	private void showDelAllDialog(){
		if(mDelAllDialg != null){
			mDelAllDialg.show();
			return;
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.wt_delete_all)
			 .setMessage(R.string.wt_del_all_contacts)
                    .setNegativeButton(android.R.string.cancel, null)
                    .setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            doDelAllContacts();
                        }
                    });
		mDelAllDialg = builder.create();
		mDelAllDialg.show();
	}

	private void doDelAllContacts(){
		ContentResolver cr = getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
		while (cur.moveToNext()) {
			try{
				String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
				Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
				cr.delete(uri, null, null);
			}catch(Exception e){
				android.util.Log.i("hcj","doDelAllContacts e="+e);
			}
		}
	}

    private final class ContactBrowserActionListener implements OnContactBrowserActionListener {
        ContactBrowserActionListener() {}

        @Override
        public void onSelectionChange() {
        }

        @Override
        public void onViewContactAction(Uri contactLookupUri) {
            Intent intent = QuickContact.composeQuickContactsIntent(WtPeopleActivity.this,
                    (Rect) null, contactLookupUri, QuickContactActivity.MODE_FULLY_EXPANDED, null);
            startActivity(intent);
        }

        @Override
        public void onDeleteContactAction(Uri contactUri) {
            ContactDeletionInteraction.start(WtPeopleActivity.this, contactUri, false);
        }

        @Override
        public void onFinishAction() {
            onBackPressed();
        }

        @Override
        public void onInvalidSelection() {
            ContactListFilter filter;
            ContactListFilter currentFilter = mAllFragment.getFilter();
            if (currentFilter != null
                    && currentFilter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) {
                filter = ContactListFilter.createFilterWithType(
                        ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS);
                mAllFragment.setFilter(filter);
            } else {
                filter = ContactListFilter.createFilterWithType(
                        ContactListFilter.FILTER_TYPE_SINGLE_CONTACT);
                mAllFragment.setFilter(filter, false);
            }
            //mContactListFilterController.setContactListFilter(filter, true);
        }
    }	
}
